package com.qf.shopping.service.impl;

import com.qf.shopping.dao.BaseDao;
import com.qf.shopping.dao.IProductDao;
import com.qf.shopping.dao.impl.ProductDaoImpl;
import com.qf.shopping.entity.Product;
import com.qf.shopping.service.IProductService;

import java.io.File;
import java.util.List;

/**
 * @author 三旬
 * @version 1.0  2022/9/18
 */

public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    IProductDao productDao = new ProductDaoImpl();
    @Override
    protected BaseDao<Product> getBaseDao() {
        return productDao;
    }


    @Override
    public Boolean delProductPngById(Integer id, String realPath) {
        //先获得要删除的商品的图片名称
        Product product = productDao.selectOneById(id);
        String pngUrl = product.getPngUrl();
        //如果数据库未存图片地址，直接返回true
        if(pngUrl == null && "".equals(pngUrl)){
            return true;
        }else{
            //获得该图片的路径
            String path = realPath+"images/download/"+pngUrl;
            File file = new File(path);
            if(file.exists()){
                file.delete();
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean updateNoPngById(Product product) {
        return productDao.updateNoPngById(product) > 0;
    }

    @Override
    public List<Product> selectProductByStatu() {
        return productDao.selectProductByStatu();
    }
}
